/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import MiniProjet.gestionnaires.GestionnaireFilms;
import MiniProjet.gestionnaires.GestionnaireGenres;
import MiniProjet.gestionnaires.GestionnaireRubriques;
import MiniProjet.modeles.Film;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.StringTokenizer;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author SaLaH
 */
@WebServlet(name = "ServletFilms", urlPatterns = {"/ServletFilms"})
public class ServletFilms extends HttpServlet {

    @EJB
    private GestionnaireFilms gestionnaireFilms;
    @EJB
    private GestionnaireGenres gestionnaireGenres;
    @EJB
    private GestionnaireRubriques gestionnaireRubriques;
    public static final String genreId = "genreId";
    public static final String rubriqueId = "rubriqueId";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
         String action = request.getParameter("action");

           if (request.getParameter("action") != null) {
         if(action.equals("chercherParTitre")){



             if (request.getParameter("titre").isEmpty()) {

                  
                    request.setAttribute("listeDesFilms", gestionnaireFilms.getAllFilms());

             }else{
                 

                    request.setAttribute("listeDesFilms", gestionnaireFilms.getFilmsByTitre(request.getParameter("titre")));
             }
              
        
             }

            

           
           }else{
             if(request.getParameter(genreId) != null){
         Integer genrId = Integer.valueOf(request.getParameter(genreId));

         if (genrId == -1) {
            request.setAttribute("listeDesFilms", gestionnaireFilms.getAllFilms());
        } else if (genrId != -1) {
            request.setAttribute("listeDesFilms", gestionnaireGenres.getGenreById(genrId).getFilms());
             request.setAttribute("genre", gestionnaireGenres.getGenreById(genrId).getNom());
        }
          }
             
              if(request.getParameter(rubriqueId) != null){
         Integer rubriquId = Integer.valueOf(request.getParameter(rubriqueId));

         if (rubriquId == -1) {
            request.setAttribute("listeDesFilms", gestionnaireFilms.getAllFilms());
        } else if (rubriquId != -1) {
            request.setAttribute("listeDesFilms", gestionnaireRubriques.getRubriqueById(rubriquId).getFilms());
            request.setAttribute("rubrique", gestionnaireRubriques.getRubriqueById(rubriquId).getNom());
        }
          }
           }

        RequestDispatcher dp = request.getRequestDispatcher("home.jsp");
        dp.forward(request, response);
        // Après un forward, plus rien ne peut être exécuté après !



    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
